package com.delivery.www.servlet;

import com.delivery.www.po.Dish;
import com.delivery.www.po.Seller;
import com.delivery.www.service.ServiceImpl.DishServiceImpl;

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import static com.delivery.www.servlet.AddDish.addDish;
import static com.delivery.www.servlet.SellerStorePage.storePage;

public class SellerDishPage {
    public static void dishesList(int storeId, Seller seller) {
        DishServiceImpl dishService = new DishServiceImpl();
        Scanner scanner = new Scanner(System.in);
        List<Dish> dishes = dishService.getDish(storeId);
        if (dishes == null) {
            System.out.println("当前店铺还没有菜品,请先添加");
            System.out.println("是否前往添加");
            System.out.println("1.是");
            System.out.println("2.否");
            int i = scanner.nextInt();
            while (i != 1 && i != 2) {
                System.out.println("输入错误！请重新输入");
                i = scanner.nextInt();
            }
            if (i == 1) {
                addDish(storeId, seller);
            } else {
                storePage(seller);
            }
        } else {
            for (Dish dish : dishes) {
                System.out.println("===========================");
                System.out.println("菜品名:" + dish.getDishName());
                System.out.println("菜品价格:" + dish.getPrice());
                System.out.println("菜品描述:" + dish.getDescribe());
                System.out.println("===========================");
            }
        }
        System.out.println("您所要进行的操作:");
        System.out.println("1.修改菜品名");
        System.out.println("2.修改菜品的价格");
        System.out.println("3.修改菜品的描述");
        System.out.println("4.删除菜品");
        System.out.println("5.增加菜品");
        System.out.println("6.返回");
        int i = scanner.nextInt();
        while (i != 1 && i != 2 && i != 3 && i != 4 && i != 5 && i != 6) {
            System.out.println("输入错误！请重新输入");
            i = scanner.nextInt();
        }
        if (i == 1) {
            boolean b = false;
            while (true) {
                System.out.println("请输入你所要修改的菜品名");
                String oldName = scanner.next();
                System.out.println("请输入新的菜品名");
                String newName = scanner.next();
                b = dishService.updateDishName(oldName, newName);
                if (b) {
                    break;
                } else {
                    System.out.println("修改失败，请重新输入");
                }
            }
            System.out.println("修改成功，即将跳转");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            dishesList(storeId, seller);
        }
        if (i == 2) {
            boolean b = false;
            while (true) {
                System.out.println("请输入你所要修改的菜品名");
                String oldName = scanner.next();
                System.out.println("请输入新的菜品价格");
                Float price = scanner.nextFloat();
                b = dishService.updateDishPrice(oldName, price);
                if (b) {
                    break;
                } else {
                    System.out.println("修改失败，请重新输入");
                }
            }
            System.out.println("修改成功，即将跳转");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            dishesList(storeId, seller);
        }
        if (i == 3) {
            boolean b = false;
            while (true) {
                System.out.println("请输入你所要修改的菜品名");
                String oldName = scanner.next();
                System.out.println("请输入新的菜品描述");
                String introduce = scanner.next();
                b = dishService.updateDishDescribe(oldName, introduce);
                if (b) {
                    break;
                } else {
                    System.out.println("修改失败，请重新输入");
                }
            }
            System.out.println("修改成功，即将跳转");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            dishesList(storeId, seller);
        }
        if (i == 4) {
            boolean b = false;
            while (true) {
                System.out.println("请输入你所要修改的菜品名");
                String oldName = scanner.next();
                b = dishService.deleteDish(oldName, storeId);
                if (b) {
                    break;
                } else {
                    System.out.println("删除失败，请重新输入");
                }
            }
            System.out.println("删除成功，即将跳转");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            dishesList(storeId, seller);
        }
        if (i == 5) {
            addDish(storeId, seller);
        }
        if (i == 6) {
            storePage(seller);
        }
    }
}
